home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher1.12 / object / STRstring.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-30  |  3.4 KB  |  207 lines

  1. /********************************************************************
  2.  * $Author: lindner $
  3.  * $Revision: 1.1 $
  4.  * $Date: 1992/12/10 23:27:52 $
  5.  * $Source: /home/mudhoney/GopherSrc/release1.11/object/RCS/STRstring.c,v $
  6.  * $Status: $
  7.  *
  8.  * Paul Lindner, University of Minnesota CIS.
  9.  *
  10.  * Copyright 1991, 1992 by the Regents of the University of Minnesota
  11.  * see the file "Copyright" in the distribution for conditions of use.
  12.  *********************************************************************
  13.  * MODULE: STRstring.c
  14.  * Implement dynamic string library functions
  15.  *********************************************************************
  16.  * Revision History:
  17.  * $Log: STRstring.c,v $
  18.  * Revision 1.1  1992/12/10  23:27:52  lindner
  19.  * gopher 1.1 release
  20.  *
  21.  *
  22.  *********************************************************************/
  23.  
  24.  
  25. #include "STRstring.h"
  26. #include "String.h"
  27. #include "Malloc.h"
  28.  
  29. /*
  30.  * Make a new string, however use supplied parameter to set it.
  31.  */
  32. String *
  33. STRnewSet(in)
  34.   char *in;
  35. {
  36.      register String *temp;
  37.      register int len;
  38.  
  39.      temp = (String *) malloc(sizeof(String));
  40.      temp->data = NULL;
  41.  
  42.      if (in == NULL)
  43.       return(temp);
  44.  
  45.      len = strlen(in) + 1;
  46.  
  47.      temp->data = (char *) malloc(len * sizeof(char*));
  48.      strcpy(temp->data, in);
  49.           temp->len = len-1;
  50.  
  51.      return(temp);
  52. }
  53.      
  54. /*
  55.  * Make a new string, don't set anything for default yet.
  56.  */
  57.  
  58. String *
  59. STRnew()
  60. {
  61.      String *temp;
  62.  
  63.      temp = (String *) malloc(sizeof(String));
  64.      temp->data = NULL;
  65.  
  66.      return(temp);
  67. }
  68.  
  69. /*
  70.  * Destroy a string
  71.  */
  72.  
  73. void
  74. STRdestroy(st)
  75.   String *st;
  76. {
  77.      if (st != NULL) {
  78.       if (st->data != NULL)
  79.            free(st->data);
  80.       free(st);
  81.      } else
  82.       perror("STRdestroy: non existant string!\n");
  83.  
  84. }
  85.  
  86.  
  87. /*
  88.  * Clear out all the crud...
  89.  */
  90.  
  91. void 
  92. STRinit(st) 
  93.   String *st;
  94. {
  95.      if (st != NULL) {
  96.       
  97.       st->len  = 0;
  98.       if (st->data != NULL)
  99.            free(st->data);
  100.       st->data = NULL;
  101.      } else
  102.       perror("STRinit, non existant string!");
  103. }
  104.  
  105. /*
  106.  * Set a string value
  107.  */
  108.  
  109. void
  110. STRset(st, str)
  111.   String *st;
  112.   char   *str;
  113. {
  114.      register int len;
  115.  
  116.      if (str == NULL)
  117.       return;
  118.  
  119.      if (*str == '\0')
  120.       len = 1;
  121.      else
  122.       len = strlen(str) + 1;
  123.  
  124.      /* Uninitialized data... */
  125.  
  126.      if (st->data == NULL) {
  127.       st->data = (char *) malloc(len);
  128.       strcpy(st->data, str);
  129.       st->len = len-1;
  130.      }
  131.  
  132.      /** Something's already there... **/
  133.  
  134.      else {
  135.       if (STRlen(st) > len)
  136.            strcpy(st->data, str);
  137.       else {
  138.            char *temp;
  139.  
  140.            temp = (char *) realloc(st->data, len);
  141.            /*** Should check for NULL ... ***/
  142.            st->data = temp;
  143.            strcpy(st->data, str);
  144.       }
  145.      }
  146. }
  147.  
  148. /*
  149.  * Add a string to the end of the string that's there already
  150.  */
  151.  
  152. String*
  153. STRcat(st, cp)
  154.   String *st;
  155.   char *cp;
  156. {
  157.      int len;
  158.      char *temp;
  159.  
  160.      if (cp == NULL)
  161.       return(NULL);
  162.      
  163.      if (STRlen(st) == 0) {
  164.       STRset(st, cp);
  165.       return(st);
  166.      }
  167.  
  168.      len = strlen(cp) + STRlen(st) + 1;
  169.  
  170.      temp = (char *) malloc(len);
  171.      strcpy(temp, STRget(st));
  172.      strcat(temp, cp);
  173.  
  174.      STRset(st, temp);
  175.      
  176.      free(temp);
  177.  
  178.      return(st);
  179. }
  180.  
  181.  
  182. int
  183. STRcmp(st1, st2)
  184.   String *st1;
  185.   String *st2;
  186. {
  187.      register char *cp1, *cp2;
  188.  
  189.      cp1 = STRget(st1);
  190.      cp2 = STRget(st2);
  191.  
  192.      if (cp1 == NULL) 
  193.       return(- !0);
  194.      else if (cp2 == NULL)
  195.       return( !0);
  196.      else
  197.       return(strcmp(cp1, cp2));
  198. }
  199.      
  200. String*
  201. STRcpy(s1, s2)
  202.   String *s1;
  203.   String *s2;
  204. {
  205.      STRset(s1, STRget(s2));
  206. }
  207.